// pages/index/index.js
// var qcloud = require('../../vendor/wafer2-client-sdk/index')
// var config = require('../../config')
// var util = require('../../utils/util.js')
const app = getApp()
var upFiles = require('../../utils/upFiles.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    upFilesBtn: true,
    upFilesProgress: false,
    upImgArr: [],
    maxUploadLen: 6,
    content:"",
    noteNowLen:0
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      return {
      title: '应急预案智能管理小助手',
      path: '/pages/nextindex/index'
    }
  },
  // 预览图片
  previewImg: function (e) {
    let imgsrc = e.currentTarget.dataset.presrc;
    let _this = this;
    let arr = _this.data.upImgArr;
    let preArr = [];
    arr.map(function (v, i) {
      preArr.push(v.path)
    })
    //   console.log(preArr)
    wx.previewImage({
      current: imgsrc,
      urls: preArr
    })
  },
  // 删除上传图片 或者视频
  delFile: function (e) {
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '您确认删除嘛？',
      success: function (res) {
        if (res.confirm) {
          let delNum = e.currentTarget.dataset.index;
          let delType = e.currentTarget.dataset.type;
          let upImgArr = _this.data.upImgArr;
          let upVideoArr = _this.data.upVideoArr;
          if (delType == 'image') {
            upImgArr.splice(delNum, 1)
            _this.setData({
              upImgArr: upImgArr,
            })
          } else if (delType == 'video') {
            upVideoArr.splice(delNum, 1)
            _this.setData({
              upVideoArr: upVideoArr,
            })
          }
          let upFilesArr = upFiles.getPathArr(_this);
          if (upFilesArr.length < _this.data.maxUploadLen) {
            _this.setData({
              upFilesBtn: true,
            })
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },
  // 选择图片或者视频
  uploadFiles: function (e) {
    var _this = this;
    upFiles.chooseImage(_this, _this.data.maxUploadLen)
    // wx.showActionSheet({
    //   itemList: ['选择图片', '选择视频'],
    //   success: function (res) {
    //     //   console.log(res.tapIndex)
    //     let xindex = res.tapIndex;
    //     if (xindex == 0) {
         
    //     } else if (xindex == 1) {
    //       upFiles.chooseVideo(_this, _this.data.maxUploadLen)
    //     }

    //   },
    //   fail: function (res) {
    //     console.log(res.errMsg)
    //   }
    // })
  },
  // 上传文件
  subFormData: function () {
    let _this = this;
    let upData = {};
    let upImgArr = _this.data.upImgArr;
    let upVideoArr = _this.data.upVideoArr;
    // _this.setData({
    //   upFilesProgress: true,
    // })
    // upData['url'] = config.service.upFiles;
    // upFiles.upFilesFun(_this, upData, function (res) {
    //   // if (res.index < upImgArr.length) {
    //   //   upImgArr[res.index]['progress'] = res.progress
    //   //   _this.setData({
    //   //     upImgArr: upImgArr,
    //   //   })
    //   // } else {
    //   //   let i = res.index - upImgArr.length;
    //   //   upVideoArr[i]['progress'] = res.progress
    //   //   _this.setData({
    //   //     upVideoArr: upVideoArr,
    //   //   })
    //   // }
    //   //   console.log(res)
    // }, function (arr) {
    //   // success
    //   console.log(arr)
    // })
  },
  //意见反馈提交
  saveFeedBack() {
    var that = this
    var params = {}
    if (!that.data.info) {
      wx.showToast({
        title: '请输入反馈意见',
        icon: 'none',
        duration: 1000
      })
      return false
    } else if (!that.data.upImgArr.length){
      wx.showToast({
        title: '请上传反馈图片',
        icon: 'none',
        duration: 1000
      })
      return false
    }
    var userInf = JSON.parse(wx.getStorageSync('user')).user
    params.userId = userInf.id
    params.userName = userInf.userName
    params.contactWay = userInf.mobileNo
    params.feedContent = that.data.info

    // console.log(that.data.upImgArr[0].path)
    //上传提交
    wx.uploadFile({
      url: app.globalData.Url+"/emergency/v1.0/feedback",
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      formData: params,
      filePath: that.data.upImgArr[0].path,
      name:"file",
      success(res) {
        var response = JSON.parse(res.data)
        if (response.meta.success) {
          wx.showToast({
            title: response.meta.message,
            icon: 'none',
            duration: 1000
          })
        }
        setTimeout(
          ()=>{
            wx.switchTab({
              url: '../usercenter/index'
            });
          },1000
        )
      },
      fail(response){
        console.log(response)
      }
    })
  },
  onUnload(){
    // wx.showModal({
    //   title: '提示',
    //   content: '您确认删除嘛？',
    //   success: function (res) {
    //     if (res.confirm) {
    //       wx.navigateTo({
    //         url: '../feedback/index'
    //       })
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   }
    // })
  },
  bindTextAreaChange(e){
    var that = this;
    var value = e.detail.value, len = parseInt(value.length); 
    if (len > that.data.noteMaxLen) return; 
    that.setData({ info: value, noteNowLen: len })
  },

  // uploadImg(e){
  //   var file = e.target.files[0]
  //   this.test = file
  //   if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  //     alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  //     return false
  //   };
  // }

})